package com.hkw.areacodechoose

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.hkw.multiplecityidpicker.bean.AddressBean
import com.hkw.multiplecityidpicker.util.InitAddressData
import com.hkw.singlecityidpicker.listener.OnOptionsSelectListener
import com.hkw.singlecityidpicker.popup.AddressUserPopup


class MainActivity : AppCompatActivity() {

    private val TAG = javaClass.simpleName
    private var addressUserPopup: AddressUserPopup? = null




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun popAddress(view: View) {


        showPickerView1()
        //showPickerView2()
    }

    private fun showPickerView1() {
        com.hkw.singlecityidpicker.builder.OptionsPickerBuilder(
            this,
            object : OnOptionsSelectListener {
                override fun onOptionsSelect(
                    address: String?,
                    province: String?,
                    city: String?,
                    area: String?,
                    areaId: String?
                ) {
                    Log.d(
                        TAG,
                        "address=${address},province=${province},city=${city},area=${area},areaCode=${areaId}"
                    )
                }

            }).setTitleBgColor(Color.WHITE)
            .build()
    }

    /**
     * 方式二
     */
    private fun showPickerView2() {
        val options1Items: ArrayList<AddressBean> = ArrayList()
        val options2Items = ArrayList<ArrayList<AddressBean.CityBean>>()
        val options3Items = ArrayList<ArrayList<ArrayList<AddressBean.CityBean.AreaBean>>>()

        val initJsonData = InitAddressData.initJsonData(this)

        options1Items.addAll(initJsonData[0] as Collection<AddressBean>)
        options2Items.addAll(initJsonData[1] as Collection<ArrayList<AddressBean.CityBean>>)
        options3Items.addAll(initJsonData[2] as Collection<ArrayList<ArrayList<AddressBean.CityBean.AreaBean>>>)

        // 弹出选择器
        val pvOptions: OptionsPickerView<*> = OptionsPickerBuilder(
            this
        ) { options1, options2, options3, v ->

            //返回的分别是三个级别的选中位置
            var tempCode = ""
            val opt1tx = if (options1Items.size > 0) options1Items[options1].pickerViewText else ""
            tempCode = if (options1Items.size > 0) options1Items[options1].value else ""
            val opt2tx = if (options2Items.size > 0
                && options2Items[options1].size > 0
            ) options2Items[options1][options2].pickerViewText else ""
            tempCode = if (options2Items.size > 0
                && options2Items[options1].size > 0
            ) options2Items[options1][options2].value else ""

            val opt3tx =
                if (options2Items.size > 0 && options3Items[options1].size > 0 && options3Items[options1][options2].size > 0
                ) options3Items[options1][options2][options3].pickerViewText else ""
            tempCode =
                if (options2Items.size > 0 && options3Items[options1].size > 0 && options3Items[options1][options2].size > 0
                ) options3Items[options1][options2][options3].value else ""
            val tx = opt1tx + opt2tx + opt3tx

            Toast.makeText(this@MainActivity, tx+ tempCode, Toast.LENGTH_SHORT).show()
        }
            .setTitleText("城市选择")
            .setDividerColor(Color.BLACK)
            .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
            .setContentTextSize(20)
            .build<Any>()

        /*pvOptions.setPicker(options1Items);//一级选择器
        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
        pvOptions.setPicker(options1Items as List<Nothing>?,
            options2Items as List<Nothing>?, options3Items as List<Nothing>?
        ) //三级选择器
        pvOptions.show()
    }
}